package com.projectplace.GUI;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.File;
import com.projectplace.R;
import com.projectplace.SingeltonPPClient;
import com.projectplace.api.UserApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactsProfile extends Activity{

	private ImageView profilePic;
	private TextView name;
	private TextView phone;
	private TextView email;
	private TextView description;
	private ImageButton callButton;
	private ImageButton smsButton;
	private ImageButton emailButton;
	private ImageButton favoriteButton;
	private Button ButtonTop;
	private String phonenumber;
	private String emailAddress;
	private String id;
	private final static String LOG_TAG = "ContactsProfile";
	
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.contactsprofile);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
		ButtonTop = (Button)findViewById(R.id.buttonTitle);
		ButtonTop.setVisibility(0);
	
		profilePic = (ImageView) findViewById(R.id.contactsProfile);
		name = (TextView) findViewById(R.id.contactsName);
		phone = (TextView) findViewById(R.id.contactsPhone);
		email = (TextView) findViewById(R.id.contactsEmail);
		description = (TextView)findViewById(R.id.contactsDescription);
		
		callButton =(ImageButton)findViewById(R.id.callButton);
		smsButton = (ImageButton)findViewById(R.id.smsButton);
		emailButton = (ImageButton)findViewById(R.id.emailButton);
		favoriteButton = (ImageButton)findViewById(R.id.favoriteButton);
		
		id = getIntent().getStringExtra("userID");
		phonenumber = getIntent().getStringExtra("phone");
		emailAddress = getIntent().getStringExtra("email");
		
		name.setText(getIntent().getStringExtra("first") + getIntent().getStringExtra("last"));
		phone.setText(phonenumber);
		email.setText(emailAddress);
		description.setText(getIntent().getStringExtra("description"));
		setProfilePic(profilePic);
		
		callButton.setImageResource(R.drawable.call);
		smsButton.setImageResource(R.drawable.message);
		emailButton.setImageResource(R.drawable.email);
		favoriteButton.setImageResource(R.drawable.favorite);
		
		callButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				onCallButtonClick();
			}
		});
		
		smsButton.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						onSmsButtonClick();
					}
		});
		
		emailButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				onEmailButtonClick();
			}
		});

		favoriteButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				onFavoriteButtonClick();
			}
		});
		
		}
	
	private void onCallButtonClick(){
		Uri call = Uri.parse("tel:" +phonenumber);
		startActivity(new Intent(Intent.ACTION_CALL,call));
	}
	
	private void onSmsButtonClick(){; 
		Intent sendIntent = new Intent(Intent.ACTION_VIEW);
		sendIntent.putExtra("address", phonenumber);
		sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent);
				
	}
	
	private void onEmailButtonClick(){
		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailAddress} );
		emailIntent.setType("message/rfc882");
		startActivity(Intent.createChooser(emailIntent, "Send mail..."));
		
	}
	
	private void onFavoriteButtonClick(){
		Log.d(LOG_TAG, "onFavoriteButtonClick");
		
	}
	
	public void onClick(View v){
		finish();
	}
	
	private void setProfilePic(ImageView v){
		Bitmap cachedBitmapFromSDCard = BitmapFactory.decodeFile(this.getFilesDir()
				.toString()
				+ File.separatorChar
				+ id + ".jpg");
		if (cachedBitmapFromSDCard != null) 
			v.setImageBitmap(cachedBitmapFromSDCard);
		else 
			UserApi.getAvatar(SingeltonPPClient.getInstance(), id, v);
	}
}
